package com.aiju.cash.ui.signbank.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.BankApi;
import com.aiju.cash.ui.enter.AdressActivity;
import com.aiju.common.Verify.EmptyVerify;
import com.aiju.common.Verify.VerifyManger;
import com.aiju.utils.L;
import com.aiju.utils.Util;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static okhttp3.Protocol.get;

/**
 * @author: 流冥
 * @date: 2017-07-13
 * @Description: 银行通道签约第一步
 */
public class BankSignGalleryFirstActivity extends BaseTitleActivity implements View.OnClickListener {
    private SettingItemEditView shop_name;
    private SettingItemEditView shop_sub_name;
    private SettingItemTextView shop_add;
    private SettingItemEditView phone;
    private SettingItemTextView time;
    private SettingItemTextView type;
    private SettingItemTextView person_info;
    private SettingItemTextView license_info;
    private SettingItemTextView account_info;
    private SettingItemTextView img;
    private Button btn;

    private String code_name = "", province_code = "", city_code = "", district_code = "", address = "", longitude = "", latitude = "";

    public static final int ADDRESS = 105;//门店地址
    public static final int TIME = 106;//营业时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_sign_gallery_first);
        initView();
    }

    void initView() {
        mAiJuTitleBarView.setCenterTitle("银行通道签约");
        shop_name = (SettingItemEditView) findViewById(R.id.shop_name);
        shop_sub_name = (SettingItemEditView) findViewById(R.id.shop_sub_name);
        shop_add = (SettingItemTextView) findViewById(R.id.shop_add);
        phone = (SettingItemEditView) findViewById(R.id.phone);
        time = (SettingItemTextView) findViewById(R.id.time);
        type = (SettingItemTextView) findViewById(R.id.type);
        person_info = (SettingItemTextView) findViewById(R.id.person_info);
        license_info = (SettingItemTextView) findViewById(R.id.license_info);
        account_info = (SettingItemTextView) findViewById(R.id.account_info);
        img = (SettingItemTextView) findViewById(R.id.img);
        btn = (Button) findViewById(R.id.commit);
        shop_name.setEditable(true);
        shop_sub_name.setEditable(true);
        phone.setEditable(true);
        shop_name.getRightView().setHint("请填写");
        shop_sub_name.getRightView().setHint("请填写");
        phone.getRightView().setHint("请填写");

        shop_add.setOnClickListener(this);
        time.setOnClickListener(this);
        type.setOnClickListener(this);
        person_info.setOnClickListener(this);
        license_info.setOnClickListener(this);
        account_info.setOnClickListener(this);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.shop_add:
                Intent intentAdress = new Intent(mContext, AdressActivity.class);
                intentAdress.putExtra("codeName", code_name);
                intentAdress.putExtra("province_code", province_code);
                intentAdress.putExtra("city_code", city_code);
                intentAdress.putExtra("district_code", district_code);
                intentAdress.putExtra("address", address);
                intentAdress.putExtra("longitude", longitude);
                intentAdress.putExtra("latitude", latitude);
                startActivityForResult(intentAdress, ADDRESS);
                break;
            case R.id.time:
                intent.setClass(mContext, SettingBusinessTimeActivity.class);
                startActivityForResult(intent, TIME);
                break;
            case R.id.type:
                break;
            case R.id.person_info:
                intent.setClass(mContext, PersonInfoCommitActivity.class);
                startActivity(intent);
                break;
            case R.id.license_info:
                intent.setClass(mContext, BusinessLicenseInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.account_info:
                intent.setClass(mContext, CalculateAccountInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.img:
                break;
            case R.id.commit:
                if (!verrify())

                    break;
        }

    }


    private boolean verrify() {

        return new VerifyManger()
                .addVerify(new EmptyVerify(shop_name.getRightText(), "请填写商户名称"))
                .addVerify(new EmptyVerify(shop_sub_name.getRightText(), "请填写招牌名称"))
                .addVerify(new EmptyVerify(check(shop_add.getRightString(), "请选择"), "请选择门店地址"))
                .addVerify(new EmptyVerify(check(phone.getRightText(), "请填写"), "请填写联系电话"))
                .addVerify(new EmptyVerify(check(time.getRightString(), "请选择"), "请选择营业时间"))
                .addVerify(new EmptyVerify(check(type., "请选择"), "请选择行业分类")).execute();
    }

    private String check(String text, String standerd) {
        return standerd.equals(text) ? "" : text;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == ADDRESS) {
            code_name = data.getStringExtra("codeName");
            address = data.getStringExtra("address");
            shop_add.getRightView().setTextColor(Util.getColor(R.color.app_text_color));
            shop_add.setRightText(code_name + address);

            longitude = data.getStringExtra("longitude");
            latitude = data.getStringExtra("latitude");
            province_code = data.getStringExtra("province_code");
            city_code = data.getStringExtra("city_code");
            district_code = data.getStringExtra("district_code");
            L.d("地址：" + latitude + longitude + code_name + address + province_code + city_code + district_code);
        } else if (requestCode == TIME) {
            String time = data.getStringExtra("time");
            this.time.setRightText(time);
        }
    }


    String[] type1;
    Map<String, String[]> son = new HashMap<>();

    /**
     * 获取行业分类数据
     */
    private void getIndestryTypeData() {
        new BankApi(mDialogHelper).getMctClass(new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                JSONObject obj = (JSONObject) jsonData.getData();
                for (Iterator<String> iterator = obj.keys(); iterator.hasNext(); ) {
                    try {
                        String key = iterator.next();
                        JSONArray array = (JSONArray) obj.get(key);
                        String[] sonTemp = new String[array.length()];
                        for (int i = 0; i < array.length(); i++) {
                            sonTemp[i] = array.optString(i);
                        }
                        son.put(key, sonTemp);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                int size = son.keySet().size();
                type1 = new String[size];
                for (int i = 0; i < size; i++) {
                }
            }
        });

    }


}
